package cn.semcorp.zhuhaizj.quartzJob;

import cn.semcorp.zhuhaizj.common.Common;
import cn.semcorp.zhuhaizj.mapper.TrainingMapper;
import cn.semcorp.zhuhaizj.pojo.dto.FlowerUserDTO;
import cn.semcorp.zhuhaizj.pojo.entity.UpdateAbleByEntity;
import cn.semcorp.zhuhaizj.pojo.vo.NewPersonPlanVO;
import cn.semcorp.zhuhaizj.pojo.vo.NewPlanVO;
import cn.semcorp.zhuhaizj.services.impl.TrainingServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static cn.semcorp.zhuhaizj.common.Common.*;


@Slf4j
public class TobeTrainingJob implements Job {

    @Autowired
    private TrainingMapper trainingMapper;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        List<FlowerUserDTO> list = trainingMapper.selectFlowerListSp(common87);
        List<FlowerUserDTO> list2 = trainingMapper.selectFlowerList();
        if (list.size() == 0){
            return;
        }
        // 定时任务待写
        log.info("定时任务:Redis人员在职年限更新-开始执行");
//        tobeUpdateAgeLimit(list2);
        log.info("定时任务:Redis人员在职年限更新-执行完成!");

        log.info("定时任务:Redis人员属性更新-开始执行");
//        tobeUpdateFormal(list2);
        log.info("定时任务:Redis人员属性更新-执行完成!");

        log.info("定时任务:人员能力评价更新-开始执行");
        tobeUpdateCompetencyAssessment(list2);
        log.info("定时任务:人员能力评价更新-执行完成!");

//        log.info("定时任务:新人育成计划更新-开始执行");
//        tobeUpdateNewPersonPlan(list2);
//        log.info("定时任务:新人育成计划更新-执行完成!");
    }


    /**
     * 监控在职年限
     */
    public void tobeUpdateAgeLimit(List<FlowerUserDTO> list){
        for (FlowerUserDTO flower : list) {
            if(isYYYYMMDD(flower.inputTime)){
                long days = Common.howMuchDaysBetween1(flower.getInputTime());
                String ageLimit = Common.transformOneNumber(Double.parseDouble(String.valueOf(days))/common93);
                trainingMapper.updateFlowerAgeLimit(flower.getId(),ageLimit);
            }
        }
    }


    /**
     * 监控评判状态
     */
    public void tobeUpdateFormal(List<FlowerUserDTO> list){
        for (FlowerUserDTO userDTO : list) {
            if (userDTO.getFormal().equals(value6)){
                continue;
            }
            if (Double.parseDouble(userDTO.getAgeLimit()) >= 0.50){
                trainingMapper.tobeUpdateFormalById(userDTO.getId(),value6);
            }
        }
    }

    @Autowired
    private TrainingServiceImpl trainingService;
    /**
     * 监控能力评定
     */
    public void tobeUpdateCompetencyAssessment(List<FlowerUserDTO> list){
        for (FlowerUserDTO user : list) {
            if (user.getWorkshop().equals(common87)){
                trainingMapper.updateCompetencyAssessment(user.getId(),common855);
                continue;
            }
            // 培训类型     合格率受波动后的
            Map<String, String> stringStringMap
                    = trainingService.selectAblePictureByName(user.getName());
            List<String> valuesList = new ArrayList<>(stringStringMap.values());
            double count = 0; // 计数器
            double countNo = 0;
            double result = 0;
            for (String values : valuesList) {
                if (isNumber(values) ){  // 判断是否培训过
                    count ++;
                    result += Double.parseDouble(values);
                }else{
                    countNo ++;
                }
            }

            String able ;
            if (countNo == 5){
                able = "无数据";
            }else{
                result = result * 0.2;
                System.out.println(result);
                // 修改能力评定值
                if (result >= 90){
                    able = common92;
                }else if (result >= 80){
                    able = common91;
                }else if (result >= 60){
                    able = common90;
                }else if (result >= 50){
                    able = common89;
                }else{
                    able = common88;
                }
            }
            trainingMapper.updateCompetencyAssessment(user.getId(),able);
        }
    }


    /**
     * 新人育成计划
     */
    public void tobeUpdateNewPersonPlan(List<FlowerUserDTO> list){
        List<FlowerUserDTO> listTobe = new ArrayList<>();
        for (FlowerUserDTO dto : list) {
            if (dto.getFormal().equals(objValue68)){
                listTobe.add(dto);
            }
        }

        List<NewPersonPlanVO> insetList = trainingMapper.selectNewPlanRecords();

        for (FlowerUserDTO dto : listTobe) {
            int i = 0 ;
            List<NewPlanVO> st = trainingMapper.selectNewPlan(String.valueOf(dto.getId()));
            if (st.size() > 0){ // 生成了新人育成计划下
                for (NewPlanVO newPlanVO : st) {
                    if (newPlanVO.getIsCompleted()){ // 判断完成
                        i ++ ;
                        if (i == st.size()){
                            // 都完成了 调整plan的值
                            trainingMapper.updateNewFlowerPlan(dto.getId(),objValue69);
                        }
                    }

                }
                System.out.println(i);
                continue; // 不进入生成计划的逻辑
            }


            int i2 = 1;
            for (NewPersonPlanVO newPersonPlanVO : insetList) {
                newPersonPlanVO.setUserId(dto.getId());
                newPersonPlanVO.setSortId(i2);
                i2++;
            }
            trainingMapper.insetNewPersonPlans(insetList);
        }
    }

    /**
     * 日期格式判断 yyyy-mm-dd
     * @param dateString
     * @return
     */
    public static boolean isYYYYMMDD(String dateString) {
        String pattern = "^\\d{4}-\\d{2}-\\d{2}$";
        return Pattern.matches(pattern, dateString);
    }

}
